package com.vitabuybuy.tv.dialog

import android.content.Context
import android.text.Html
import android.view.View
import android.view.animation.Animation
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.util.DateUtil
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemLimitedGroupBinding
import com.vitabuybuy.tv.databinding.PopupAllLimitedGroupBinding
import com.vitabuybuy.tv.dto.GroupBuyMemberDto
import com.library.common.base.BindRecycAdapter
import com.library.common.ext.mmkv
import com.library.common.toast.XToast
import com.library.common.util.MMKVContancts
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

/**
 * 全部拼单弹出框
 */
class AllLimitedGroupPopup
    (
    context: Context,
    memberlist: MutableList<GroupBuyMemberDto>,
    private var onConfirm: (memberDto: GroupBuyMemberDto) -> Unit
) : BasePopupWindow(context) {

    private lateinit var limitedGroupListAdapter: LimitedGroupListAdapter

    private lateinit var mBinding: PopupAllLimitedGroupBinding

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_all_limited_group)
    }

    init {
        limitedGroupListAdapter.setNewData(memberlist)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupAllLimitedGroupBinding

        setViewClickListener(View.OnClickListener { this.dismiss() }, mBinding.ivClose)
        limitedGroupListAdapter = LimitedGroupListAdapter()
        mBinding.recyclerView.apply {
            adapter = limitedGroupListAdapter
            layoutManager = LinearLayoutManager(context)
        }

        limitedGroupListAdapter.setOnItemClickListener { _, item, _ ->
            if(mmkv.decodeInt(MMKVContancts.USER_ID) == item.creator.id){
                XToast.normal(context.getString(R.string.you_are_join_this_group_buy))
                return@setOnItemClickListener
            }
            onConfirm(item)
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toDismiss()
    }

    class LimitedGroupListAdapter : BindRecycAdapter<ItemLimitedGroupBinding, GroupBuyMemberDto>() {
        override fun onBindViewHolder(
            mBinding: ItemLimitedGroupBinding,
            itemData: GroupBuyMemberDto,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                mBinding.tvShortOfPeople.text = Html.fromHtml(
                    String.format(
                        mContext.getString(R.string.short_of_people),
                        itemData.creator.nickname,
                        (itemData.memberLimit - itemData.memberQuantity)
                    ),
                    Html.FROM_HTML_MODE_COMPACT
                )
            }
            mBinding.tvRemainingTime.text =
                String.format(
                    mContext.getString(R.string.remaining_time),
                    DateUtil.formatSeconds(itemData.remainingTime)
                )

            ImageAdapter.setCircleImageUrl(
                mBinding.ivAvatar,
                itemData.creator.avatar,
                ContextCompat.getDrawable(mContext, R.drawable.wode_icon_touxiang)
            )
        }

        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_limited_group
        }
    }
}